package net.zabuchy.meeting.action;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;

import net.zabuchy.address.entity.Address;
import net.zabuchy.helper.SessionHelper;
import net.zabuchy.issue.entity.Issue;
import net.zabuchy.meeting.bo.MeetingBo;
import net.zabuchy.meeting.entity.Meeting;
import net.zabuchy.user.bo.UserBo;
import net.zabuchy.user.entity.User;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

import freemarker.template.utility.StringUtil;

public class MeetingAction extends ActionSupport
	implements ServletRequestAware,ServletResponseAware{ 
	
	@Autowired
	MeetingBo meetingBo;
	
	@Autowired
	UserBo userBo;
	
	@Autowired
    private SessionHelper sessionBean;
	
	@Autowired
	private Address address;
	
	private Integer meetingId;
	private Integer customerId;
	private String author;
	private String title;
	private String description;
	private String leading;
	private Date creationDate;
	private Date meetingDate;
	private String place;
	
	private Date date0;
	
	private String tmpParticipants;
	
	private Set<User> participants = new HashSet<User>(0);
	
	
	private HttpServletRequest request;
    private HttpServletResponse response;
	
	// byc moze da sie to jakos zastapic
	
	
	List<Meeting> meetings;

	private static final Logger LOGGER = Logger.getLogger(MeetingAction.class.getName());
	
	public String createMeeting() {		
		
		LOGGER.debug("addForm");
		String returnInputIssueForm = request.getParameter("returnInputMeetigForm");
		
		boolean returnForm = (returnInputIssueForm != null ? 
				Boolean.parseBoolean(returnInputIssueForm) : false);
		
		if(returnForm) {
			LOGGER.debug("clear all fields and return form");
			//clearFields();
			return Action.INPUT;
		}

		if(date0 != null)
			LOGGER.debug("date0: " + date0.toString());
		
		Meeting meeting = new Meeting();
		meeting.setTitle(title);
		meeting.setDescription(description);
		meeting.setCreationDate(creationDate);
		meeting.setMeetingDate(meetingDate);
		meeting.setPlace(place);
		meeting.setParticipants(participants);
		meeting.setLeading(leading);
		
		
		///// tylko chwilowe
		String[] particips = StringUtil.split(tmpParticipants, ',');
		User user;
		for(String userId : particips) {
			user = userBo.findById(Integer.parseInt(userId));
			participants.add(user);
		}
		meeting.setParticipants(participants);
		///// tylko chwilowe
		
		LOGGER.debug("createMetting");
		if(address != null)
			LOGGER.debug("createMetting not null address");
		meeting.setAddress(address);
		/*address.setCity(address.getCity() );
		meeting.setAddress(address);*/
		meetingBo.add(meeting);
		
		return Action.SUCCESS;
	}
	
	public String listMy() {
		
		if (meetingBo == null) {
            return Action.ERROR;
        }
        LOGGER.debug("Get all meetings");
        
        User  user = sessionBean.getExecutor();
        meetings = new ArrayList<Meeting>(0);
        for(Meeting meet : user.getMeetings()) {
        	meetings.add(meet);
        }
        LOGGER.debug("Meetings number = " + meetings.size());
        return Action.SUCCESS;
		
	}
	
	public String listAll() {
		
		if (meetingBo == null) {
            return Action.ERROR;
        }
        LOGGER.debug("Get all meetings");
        
        meetings = meetingBo.findAll();
        LOGGER.debug("Meetings number = " + meetings.size());
        return Action.SUCCESS;
		
	}
	
	public String delete() {
        if (meetingBo == null) {
            return Action.ERROR;
        }
 
        Meeting meeting = (Meeting) meetingBo.findById(meetingId);
        if (meeting == null) {
            return Action.ERROR;
        }
 
        LOGGER.debug("Delete meeting " + meeting.getTitle());
        meetingBo.delete(meeting);
        return Action.SUCCESS;
    }
	
	
	
	
	public MeetingBo getMeetingBo() {
		return meetingBo;
	}

	public void setMeetingBo(MeetingBo meetingBo) {
		this.meetingBo = meetingBo;
	}

	public SessionHelper getSessionBean() {
		return sessionBean;
	}

	public void setSessionBean(SessionHelper sessionBean) {
		this.sessionBean = sessionBean;
	}


	public Integer getCustomerId() {
		return customerId;
	}


	public String getTitle() {
		return title;
	}


	public String getDescription() {
		return description;
	}


	public Address getAddress() {
		return address;
	}


	public void setCustomerId(Integer customerId) {
		this.customerId = customerId;
	}


	public void setTitle(String title) {
		this.title = title;
	}


	public void setDescription(String description) {
		this.description = description;
	}


	public void setAddress(Address address) {
		this.address = address;
	}

	public Integer getMeetingId() {
		return meetingId;
	}

	public List getMeetings() {
		return meetings;
	}

	public void setMeetingId(Integer meetingId) {
		this.meetingId = meetingId;
	}

	public void setMeetings(List meetings) {
		this.meetings = meetings;
	}

	public Date getCreationDate() {
		return creationDate;
	}

	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}

	public Date getMeetingDate() {
		return meetingDate;
	}

	public void setMeetingDate(Date meetingDate) {
		this.meetingDate = meetingDate;
	}

	public String getPlace() {
		return place;
	}

	public void setPlace(String place) {
		this.place = place;
	}

	public String getLeading() {
		return leading;
	}

	public void setLeading(String leading) {
		this.leading = leading;
	}

	public Set<User> getParticipants() {
		return participants;
	}

	public void setParticipants(Set<User> participants) {
		this.participants = participants;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getAuthor() {
		return author;
	}

	public void setTmpParticipants(String tmpParticipants) {
		this.tmpParticipants = tmpParticipants;
	}

	public String getTmpParticipants() {
		return tmpParticipants;
	}

	public UserBo getUserBo() {
		return userBo;
	}

	public void setUserBo(UserBo userBo) {
		this.userBo = userBo;
	}

	/**
	 * @param date0 the date0 to set
	 */
	public void setDate0(Date date0) {
		this.date0 = date0;
	}

	/**
	 * @return the date0
	 */
	public Date getDate0() {
		return date0;
	}
	public void setServletRequest(HttpServletRequest request){
		  this.request = request;
		  }

		  public HttpServletRequest getServletRequest(){
		  return request;
		  }

		  public void setServletResponse(HttpServletResponse response){
		  this.response = response;
		  }

		  public HttpServletResponse getServletResponse(){
		  return response;
		  }
}